package com.rrd.itext.utils.color;

import java.awt.Color;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;

public class ColorUtil {
    private ICC_ColorSpace ICC_ClSpace;
    
    private static ColorUtil self;
    
    private ColorUtil() {
    }
    
    private void initICC_ClSpace(){
        if(ICC_ClSpace != null){
            return;
        }
        String filename = ColorUtil.class.getResource("") + "CMYK.pf";
        filename = filename.substring(filename.indexOf(":/")+2);
        //Get ICC From pfFile;
        try {
            ICC_Profile ICC_pf = ICC_Profile.getInstance(filename);
            ICC_ClSpace = new ICC_ColorSpace(ICC_pf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static ColorUtil getInstance(){
        if(self == null){
            self = new ColorUtil();
        }
        return self;
    }
    
    public static void main(String[] args) {
        System.out.println();
        System.out.println("RGB Color: 255,255,255 --> CMYK Color: " + array2string(ColorUtil.getInstance().RGB2CMYK(255, 255, 255)));
        System.out.println("RGB Color: 0,0,0 --> CMYK Color: " + array2string(ColorUtil.getInstance().RGB2CMYK(0, 0, 0)));
        System.out.println("RGB Color: 255,0,0 --> CMYK Color: " + array2string(ColorUtil.getInstance().RGB2CMYK(255, 0, 0)));
    }
    
    private static String array2string(float[] arr){
        String result = ",";
        for(float a : arr){
            result = result + "," +  a;
        }
        return result.substring(1);
    }
    
    public float[] RGB2CMYK(Color rgbColor) {
        if(ICC_ClSpace == null){
            this.initICC_ClSpace();
        }
        float[] CMYKfloat = ICC_ClSpace.fromRGB(rgbColor.getRGBComponents(null));
        return CMYKfloat;
    }
    
    public float[] RGB2CMYK(int colorR, int colorG, int colorB){
        float color[] = new float[4];
        float c = 1.0F - ((float)colorR)/255;
        float m = 1.0F - ((float)colorG)/255;
        float y = 1.0F - ((float)colorB)/255;
        float k = Math.min(c, Math.min(m, y));
        float ik = 1.0F - k;
        if (ik == 0.0F) {
            c = 0.0F;
            m = 0.0F;
            y = 0.0F;
        } else {
            c = (c - k) / ik;
            m = (m - k) / ik;
            y = (y - k) / ik;
        }
        color[0] = c;
        color[1] = m;
        color[2] = y;
        color[3] = k;
        return color;
    }
    
    public Color CMYK2RGB(float[] CMYKfloat) {
        if(ICC_ClSpace == null){
            this.initICC_ClSpace();
        }
        Color RGBColor = new Color(ICC_ClSpace, CMYKfloat, 1.0f);
        return RGBColor;
    }
    
    public int[] CMYK2RGB(float c, float m, float y, float k) {
            int color[] = new int[3];
            float ik = 1.0F - k;
            color[0] = (int)((1.0F - (c * ik + k)*255));
            color[1] = (int)((1.0F - (m * ik + k)*255));
            color[2] = (int)((1.0F - (y * ik + k)*255));
            return color;
    }
}
